package com.bw.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.mapper.UserMapper;
import com.bw.pojo.User;
import com.bw.utils.ResultResponse;
import com.bw.utils.TokenUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vo.UserInfoVo;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 小怪兽
 * @version 1.0
 * @description: TODO
 * @date 2022/1/6 7:59
 */
@Service
@Transactional
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper mapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ResultResponse list(User user) {
        List<User> list =mapper.selectList(null);
        return ResultResponse.SUCCESS(list);
    }

    @Override
    public ResultResponse add(User user) {
        int insert = mapper.insert(user);

        return ResultResponse.SUCCESS(insert);
    }

    @Override
    public ResultResponse del(User user) {
        int i = mapper.deleteById(user.getUid());
        return ResultResponse.SUCCESS(i);
    }

    @Override
    public ResultResponse update(User user) {
        int i = mapper.updateById(user);

        return ResultResponse.SUCCESS(i);
    }

    @Override
    public ResultResponse login(User user) {
        User us = mapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getName,user.getName()));
        if(us==null){
            return ResultResponse.FAILED(404,"用户名不存在");
        }else{
            if(us.getPassword().equals(user.getPassword())){
                //生成token
                String token = TokenUtils.token().setKey("123456").setExpire(60 * 30).createToken("uid",""+user.getUid(),"name",user.getName());
                //把token存放在redis
                String key="pc:"+user.getUid();
                redisTemplate.opsForValue().set(key,token,30, TimeUnit.MINUTES);
                redisTemplate.opsForValue().set(token,token,30,TimeUnit.MINUTES);
                //返回用户信息
                UserInfoVo userInfoVo = new UserInfoVo();
                //从user对象拷贝到userinfo里
                BeanUtils.copyProperties(user,userInfoVo);
                userInfoVo.setToken(token);
                return ResultResponse.SUCCESS(userInfoVo);

            }else{
                return ResultResponse.FAILED(403,"密码错误");
            }
        }
    }

    @Override
    public ResultResponse resetPassword(User user) {
        User us = mapper.selectById(user.getUid());
        if(us==null){
            return ResultResponse.FAILED(404,"用户不存在");
        }else{
            if(us.getPassword().equals(user.getPassword())){
                us.setPassword(user.getNewpassword());
                mapper.updateById(us);
                return ResultResponse.SUCCESS();
            }else{
                return ResultResponse.FAILED(403,"密码错误");
            }
        }
    }
}
